using System;
using System.Collections.Generic;
using System.Text;
using DM.Entity;
using System.Data.SqlClient;
using System.Data;

namespace DM.DAO
{
    public class NsdDAO:BaseDao<NSD>
    {
        public override NSD Get(int id)
        {
            NSD nsd = new NSD();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getNSDByMa", id);
            try
            {
                while (reader.Read())
                {
                    nsd.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    nsd.Manhomnsd = Common.ParseInt32(reader["MaNhomNSD"].ToString());
                    nsd.Tendangnhap = reader["TenDangNhap"].ToString();
                    nsd.Matkhau = reader["MatKhau"].ToString();
                    nsd.Hoten = reader["HoTen"].ToString();
                    nsd.Dienthoai = reader["DienThoai"].ToString();
                    nsd.Didong = reader["DiDong"].ToString();
                    nsd.Email = reader["Email"].ToString();
                    nsd.Diachi = reader["DiaChi"].ToString();
                    nsd.Congty = reader["CongTy"].ToString();
                    nsd.Yahoo = reader["Yahoo"].ToString();
                    nsd.Msn = reader["MSN"].ToString();
                    nsd.Skype = reader["Skype"].ToString();
                }
            }
            catch (Exception)
            {
                Common.Show("Error Connection!");
                throw;
            }
           finally {reader.Close();}
            return nsd;
        }
        /// <summary>
        /// Trả lại một Entity NSD (Người sử dụng)
        /// </summary>
        /// <param name="tendangnhap">Tên đăng nhập</param>
        /// <returns>NSD Entity</returns>
        public override NSD Get(string tendangnhap)
        {
            NSD nsd = new NSD();
            SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DMConnnectString, "getNSDByName",tendangnhap);
            try
            {
                while (reader.Read())
                {
                    nsd.Ma = Common.ParseInt32(reader["Ma"].ToString());
                    nsd.Manhomnsd = Common.ParseInt32(reader["MaNhomNSD"].ToString());
                    nsd.Tendangnhap = reader["TenDangNhap"].ToString();
                    nsd.Matkhau = reader["MatKhau"].ToString();
                    nsd.Hoten = reader["HoTen"].ToString();
                    nsd.Dienthoai = reader["DienThoai"].ToString();
                    nsd.Didong = reader["DiDong"].ToString();
                    nsd.Email = reader["Email"].ToString();
                    nsd.Diachi = reader["DiaChi"].ToString();
                    nsd.Congty = reader["CongTy"].ToString();
                    nsd.Yahoo = reader["Yahoo"].ToString();
                    nsd.Msn = reader["MSN"].ToString();
                    nsd.Skype = reader["Skype"].ToString();
                }
            }
            catch (Exception)
            {
                Common.Show("Error Connection!");
                throw;
            }
            finally {reader.Close();}
            return nsd;
        }

        public override int Insert(NSD t)
        {
            int i = 0;
            SqlHelper.ExecuteNonQuery(SqlHelper.DMConnnectString, "InsertNSD", t.Manhomnsd, t.Tendangnhap, Common.HashMD5(t.Matkhau),
                                      t.Hoten, t.Dienthoai, t.Didong, t.Email, t.Diachi, t.Congty, t.Yahoo, t.Msn,
                                      t.Skype,i);
            return i;
        }

        public override int Update(NSD t)
        {
            int i = 0;
            SqlHelper.ExecuteNonQuery(SqlHelper.DMConnnectString, "UpdateNSD", t.Ma, t.Manhomnsd, t.Tendangnhap, Common.HashMD5(t.Matkhau),
                                      t.Hoten, t.Dienthoai, t.Didong, t.Email, t.Diachi, t.Congty, t.Yahoo, t.Msn,
                                      t.Skype);
            return i;
        }

        public override int Update(List<NSD> ts)
        {
            throw new NotImplementedException();
        }

        public override int Delete(string id)
        {
            int maNsd = Common.ParseInt32(id);
            SqlHelper.ExecuteNonQuery(SqlHelper.DMConnnectString, "DeleteNSDByMa", maNsd);
            return 0;
        }
        public DataSet Get()
        {
            DataSet dataSet = SqlHelper.ExecuteDataset(SqlHelper.DMConnnectString, "getNSDAll");
            return dataSet;
        }
    }
}
